<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="skuCode">
        <el-input
          v-model="queryParams.skuCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['system:warehousRecordCopy1:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehousRecordList" @selection-change="handleSelectionChange" border
              stripe>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="进出库委托单号" align="center" prop="consignmentNo"/>
      <el-table-column label="委托参考号" align="center" prop="refNo"/>
      <el-table-column label="商品编码" align="center" prop="skuCode"/>
      <el-table-column label="进/出商品数量" align="center" prop="skuPlanQty"/>
      <el-table-column label="实际进出数量" align="center" prop="skuReceivedQty"/>
      <el-table-column label="进/出库状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.in_out_status" :value="scope.row.status"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="采购凭证" align="center" prop="proof">
        <template slot-scope="scope">
          <image-preview :src="scope.row.proof" width="60" height="60"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="采购金额" align="center" prop="purchasePrice"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改FPX仓储记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商品编码" prop="skuCode">
          <el-input v-model="form.skuCode" placeholder="请输入商品编码"/>
        </el-form-item>
        <el-form-item label="商品数量" prop="productCount">
          <el-input v-model="form.productCount" placeholder="请输入商品数量"/>
        </el-form-item>
        <el-form-item label="国内运单号" prop="trackingNo">
          <el-input v-model="form.trackingNo" placeholder="请输入国内运单号"/>
        </el-form-item>
        <el-form-item label="购买凭证：" prop="proof">
          <image-upload
            v-model="form.proof"
            :is-show-tip="false"
            width="50px"
            height="50px"
          />
        </el-form-item>
        <el-form-item label="预计到货时间" prop="arrivalTime">
          <el-date-picker
            clearable
            v-model="form.arrivalTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计到货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采购成本" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入采购成本"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getWarehousRecords, sendOutGoodsToFPX
} from "@/api/warehouse/warehouse";
import Dict from "@/views/system/dict/index.vue";

export default {
  name: "WarehousRecordCopy1",
  dicts: ['in_out_status'],
  components: {Dict},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // FPX仓储记录表格数据
      warehousRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skuCode: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        skuCode: [{required: true, message: "商品编码不能为空", trigger: "blur"}],
        productCount: [{required: true, message: "商品数量不能为空", trigger: "blur"}],
        arrivalTime: [{required: true, message: "预计到货时间不能为空", trigger: "change"}],
        trackingNo: [{required: true, message: "运单号不能为空", trigger: "change"}],
        proof: [{required: true, message: "采购凭证不能为空", trigger: "blur"}],
        purchasePrice: [{required: true, message: "采购金额不能为空", trigger: "blur"}],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询FPX仓储记录列表 */
    getList() {
      this.loading = true;
      getWarehousRecords(this.queryParams).then(response => {
        this.warehousRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        skuCode: undefined,
        proof: undefined,
        productCount: undefined,
        purchasePrice: undefined,
        arrivalTime: undefined,
        trackingNo: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加FPX仓储记录";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          let time = new Date(this.form.arrivalTime)
          this.form.arrivalTime = time.getTime()
          sendOutGoodsToFPX(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/warehousRecordCopy1/export', {
        ...this.queryParams
      }, `warehousRecordCopy1_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
